//
//  PXHCountDownButton.m
//  CountDownButton
//
//  Created by YiXuePxh on 15/5/13.
//  Copyright (c) 2015年 上海益学. All rights reserved.
//

#import "PXHCountDownButton.h"

@implementation PXHCountDownButton
#pragma -mark touche action
-(void)addTuochedHandler:(TouchedDownBlock)touchHandler{
    _touchedDownBlock = [touchHandler copy];
    [self addTarget:self action:@selector(touched:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)touched:(PXHCountDownButton* )sender{
    if (_touchedDownBlock) {
        _touchedDownBlock(sender,sender.tag);
    }
}
#pragma -mark count down method
-(void)startWithSecond:(int)totalSecond BgImage:(NSString* )bgImage{
    self.enabled = NO;
    _totalSecond = totalSecond;
    _second = totalSecond;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerStart:) userInfo:nil repeats:YES];
    _startDate = [NSDate date];
    [self setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateSelected];
    NSString* title = [NSString stringWithFormat:@"剩余%d秒",totalSecond];
    [self setTitle:title forState:UIControlStateNormal];
    [self setTitle:title forState:UIControlStateSelected];
    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
}
-(void)timerStart:(NSTimer* )theTimer{
    double deltaTime = [[NSDate date] timeIntervalSinceDate:_startDate];
    _second = _totalSecond - (int)(deltaTime + 0.5);
    NSLog(@"_second%.d",_second);
    NSLog(@"deltaTime%.f",deltaTime);
    NSLog(@"int deltaTime%.d",(int)deltaTime);
    NSLog(@"%d",(int)(deltaTime + 0.5));
    if (_second < 0.0) {
        [self stop];
    }else{
        if (_didChangedBlock) {
            [self setTitle:_didChangedBlock(self,_second) forState:UIControlStateNormal];
            [self setTitle:_didChangedBlock(self,_second) forState:UIControlStateSelected];
        }else{
            NSString* title = [NSString stringWithFormat:@"%d秒",_second];
            [self setTitle:title forState:UIControlStateNormal];
            [self setTitle:title forState:UIControlStateSelected];
        }
    }
}
-(void)stop{
    self.enabled = YES;
    if (_timer) {
        if ([_timer respondsToSelector:@selector(isValid)]) {
            if ([_timer isValid]) {
                [_timer invalidate];
                _second = _totalSecond;
                if (_didFinishBlock) {
                    [self setTitle:_didFinishBlock(self,_totalSecond) forState:UIControlStateNormal];
                    [self setTitle:_didFinishBlock(self,_totalSecond) forState:UIControlStateSelected];
                }else{
                    [self setTitle:@"重新获取" forState:UIControlStateNormal];
                    [self setTitle:@"重新获取" forState:UIControlStateSelected];
                }
            }
        }
    }
}
#pragma -mark blok
-(void)didChange:(DidChangedBlock)didChangeBlock{
    _didChangedBlock = [didChangeBlock copy];
}
-(void)didFinished:(DidFinishedBlock)didFinishBlock{
    _didFinishBlock = [didFinishBlock copy];
}
-(void)beginWithTitle:(NSString* )title BgImage:(NSString* ) bgImage{
    [self setTitle:title forState:UIControlStateNormal];
    [self setTitle:title forState:UIControlStateSelected];
    [self setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateSelected];
}
-(NSString* )changeWithTitle:(NSString* ) title BgImgae:(NSString* )bgImage{
    [self setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateSelected];
    return title;
}
-(NSString* )finishWithTitle:(NSString* ) title BgImgae:(NSString* )bgImage{
    self.enabled = YES;
    [self setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateSelected];
    return title;
}

@end
